﻿using _698._45Tool.Utils;
using nblTool.utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Jn698._45Tool.Windows
{
    public partial class Comm : Form
    {
        public Comm()
        {
            InitializeComponent();
            combType.Items.AddRange(new object[] { "串口", "GPRS" });
            combPort.Items.AddRange(SerialPortHelper.GetPortArray());
            combBaudrate.Items.AddRange(new object[] { 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 115200 });
            combDatabits.Items.AddRange(new object[] { 5, 6, 7, 8 });
            combStopBits.Items.AddRange(new object[] { 1, 1.5, 2 });
            combParity.Items.AddRange(new object[] { "无校验", "奇校验", "偶校验" });
            combType.SelectedIndex = 0;
        }

        public Action<string> ChangeMsg;
        private int selectedPort = 0;
        public static string selectedCommTypeName;
        public static bool isOpenComm = false;
        private SerialPortHelper serialPortHelper = SerialPortHelper.Instance;
        private TcpHelper tcpHelper = TcpHelper.Instance;
        private void GetCombData()
        {
            string combTypeName = combType.SelectedItem.ToString();
            switch (combTypeName)
            {
                case "串口":
                    combType.SelectedIndex = 0;
                    combPort.Visible = true;
                    textBoxPort.Visible = false;
                    combParity.SelectedIndex = 2;
                    combPort.SelectedIndex = selectedPort;
                    combBaudrate.SelectedIndex = 3;
                    combStopBits.SelectedIndex = 0;
                    combDatabits.SelectedIndex = 3;
                    textPort.Text = "串口号:";
                    break;
                case "GPRS":
                    combPort.Visible = false;
                    textBoxPort.Visible = true;
                    combParity.SelectedIndex = 0;
                    combBaudrate.SelectedIndex = 3;
                    combStopBits.SelectedIndex = 0;
                    combDatabits.SelectedIndex = 3;
                    textPort.Text = "端口号:";
                    break;
                default:
                    break;
            }
        }
        private Parity GetSelectedParity()
        {
            switch (combParity.SelectedItem.ToString())
            {
                case "奇校验":
                    return Parity.Odd;
                case "偶校验":
                    return Parity.Even;
                case "无校验":
                default:
                    return Parity.None;
            }
        }

        private StopBits GetSelectedStopBits()
        {
            switch (Convert.ToDouble(combStopBits.SelectedItem))
            {
                case 1:
                    return StopBits.One;
                case 1.5:
                    return StopBits.OnePointFive;
                case 2:
                    return StopBits.Two;
                default:
                    return StopBits.One;
            }
        }

        private void OK_Click(object sender, EventArgs e)
        {
            selectedCommTypeName = combType.SelectedItem.ToString();
            if (serialPortHelper.Status)
            {
                serialPortHelper.Close();
            }
            switch (selectedCommTypeName)
            {
                case "串口":
                    try
                    {

                        //串口号
                        string port = combPort.SelectedItem.ToString();
                        //波特率
                        int baudrate = (int)combBaudrate.SelectedItem;
                        //奇偶校验
                        Parity parity = GetSelectedParity();
                        //数据位
                        int databits = (int)combDatabits.SelectedItem;
                        //停止位
                        StopBits stopBits = GetSelectedStopBits();

                        //设定串口参数
                        serialPortHelper.SetSerialPort(port, baudrate, parity, databits, stopBits);
                       
                    }
                    catch (Exception e1)
                    {
                        MessageBox.Show(e1.Message);
                       
                        return;
                    }
                    break;
                case "GPRS":
                    //tcpHelper.OpenTCP(Convert.ToInt16(textBoxPort));
                    break;
                default:
                    break;
            }
           
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void combType_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetCombData();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        
    }
}
